package com.guyaning.deyson_ec.ec.main.index;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;

import com.guyaning.deyson_core.core.ui.recycler.RgbValue;
import com.guyaning.deyson_ec.ec.R;

/**
 * Created by Administrator on 2018/6/26.
 */

@SuppressWarnings("unused")
public class TransLuncentBehavior extends CoordinatorLayout.Behavior<Toolbar> {

    //距离顶部的距离
    private int mDistnceY = 0;

    //颜色变化的速率
    private static final int SHOW_SPEED = 3;

    //定义变化的颜色
    private final RgbValue RGB_VALUE = RgbValue.create(255, 124, 2);


    //重写两个参数的构造方法，否则会报错
    public TransLuncentBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //所要依赖的view
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, Toolbar child, View dependency) {
        return dependency.getId() == R.id.rl_index;
    }

    //接管该事件
    @Override
    public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
        return true;
    }

    @Override
    public void onNestedPreScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull Toolbar child, @NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);

        //增加滑动距离
        mDistnceY += dy;

        //获取Toolbar的高度
        int targetHeight = child.getBottom();

        //当滑动的距离，大于0并且小于toolbar的高度的时候，调整渐变色
        if (mDistnceY > 0 && mDistnceY <= targetHeight) {
            float sacle = (float) mDistnceY / targetHeight;

            float alpha = sacle * 255;

            child.setBackgroundColor(Color.argb((int) alpha, RGB_VALUE.red(), RGB_VALUE.green(), RGB_VALUE.blue()));
        } else if (mDistnceY > targetHeight) {
            child.setBackgroundColor(Color.rgb(RGB_VALUE.red(), RGB_VALUE.green(), RGB_VALUE.blue()));
        }

    }

}
